﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Clotho.PlotTable {
	public partial class PlotTableControl : UserControl, UI.StudioControl {
		public readonly Novel Nov;
		public Func<string> dGetCreateString { get; set; }
		public static void injectTo(List<UI.ControlKeeper> controlKeepers, Novel nov) {
			var ck = new UI.ControlKeeper(crt_str => new PlotTableControl(nov), "全部情节", () => "全部情节","Table");
			controlKeepers.Add(ck);
		}
		public PlotTableControl() {
			InitializeComponent();
		}
		public PlotTableControl(Novel nov) {
			InitializeComponent();
			Nov = nov;
			foreach (var p in Nov) {
				addPlotControl(p);
			}
			Nov.eAddPlot += Nov_eAddPlot;
			Nov.eRemovePlot += Nov_eRemovePlot;
			this.Disposed += (sender, e) => {
				Nov.eAddPlot -= Nov_eAddPlot;
				Nov.eRemovePlot -= Nov_eRemovePlot;
			};
		}
		public ContextMenuStrip HeadContextMenuStrip => null;

		private void Nov_eRemovePlot(object sender, EventArgs e) {
			DataGridViewRow target_row = null;
			foreach (DataGridViewRow row in mainDGV.Rows) {
				if (row.Tag == sender) {
					target_row = row;
					break;
				}
			}
			if (target_row != null) {
				mainDGV.Rows.Remove(target_row);
			}
		}

		private void Nov_eAddPlot(object sender, EventArgs e) {
			addPlotControl(sender as Plot);
		}

		public string Create_str => "全部情节";

		string MutFormStudio.StudioControl.Head_text { get => "全部情节"; set { } }
		public Action dHeadTestChanged { get; set; }


		public Color Head_color => Color.LightGray;



		private void addPlotControl(Plot plot) {
			int row_num = mainDGV.Rows.Add();
			DataGridViewRow dgvr = mainDGV.Rows[row_num];
			dgvr.Cells[0].Value = plot.Name;
			dgvr.Cells[1].Value = plot.Description;
			plot.eNameChanged += Plot_eNameChanged;
			plot.eDescriptionChanged += Plot_eDescriptionChanged;
			plot.eSelectChange += Plot_eSelectChange;
			plot.eRemoved += Plot_eRemoved;
			dgvr.Tag = plot;
			//关闭本窗体后，要去掉三个事件，也可以在事件里去掉
			this.Disposed += Plot_eRemoved;
			void Plot_eSelectChange(object sender, EventArgs e) {
				var p = sender as Plot;
				dgvr.Cells[1].Selected = p.Selected;

				//mainDGV.CurrentCell = dgvr.Cells[1];
			}
			void Plot_eRemoved(object sender, EventArgs e) {
				plot.eNameChanged -= Plot_eNameChanged;
				plot.eDescriptionChanged -= Plot_eDescriptionChanged;
				plot.eSelectChange -= Plot_eSelectChange;
				plot.eRemoved -= Plot_eRemoved;
			}
		}


		private void Plot_eDescriptionChanged(object obj, EventArgs e) {
			foreach (DataGridViewRow row in mainDGV.Rows) {
				if (row.Tag == obj) {
					row.Cells[1].Value = (obj as Plot).Description;
					return;
				}
			}
		}

		private void Plot_eNameChanged(object obj, EventArgs e) {
			foreach (DataGridViewRow row in mainDGV.Rows) {
				if (row.Tag == obj) {
					row.Cells[0].Value = (obj as Plot).Name;
					return;
				}
			}
		}

		private void mainDGV_SelectionChanged(object sender, EventArgs e) {
			foreach (DataGridViewRow dgvr in mainDGV.Rows) {
				Plot p = dgvr.Tag as Plot;
				if (p != null) {
					p.Selected = dgvr.Cells[1].Selected;
				}
			}
		}

		private void mainDGV_DragLeave(object sender, EventArgs e) {

		}

		private void mainDGV_CellMouseMove(object sender, DataGridViewCellMouseEventArgs e) {
			if ((MouseButtons == MouseButtons.Left)
				&& (e.ColumnIndex == 0)&&(e.RowIndex!=-1)) {
				var dgvr = mainDGV.Rows[e.RowIndex];
				Plot p = dgvr.Tag as Plot;
				DragDropEffects dde = DoDragDrop(p, DragDropEffects.Move);
			}
		}
	}
}
